#include "facetracker.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

FaceTracker::FaceTracker()
{
    bool haarLoaded = true;

    face_cascade_name = "haarcascade_frontalface_alt.xml";

    if( !face_cascade.load( face_cascade_name ) )
    {
        haarLoaded = false;
        printf("Error loading\n");
    };
}

FaceTracker::~FaceTracker()
{
}

void FaceTracker::detect ( cv::Mat frame )
{
    vector<Rect> faces;
    Mat frame_gray;

    baseImage = frame;

    cvtColor( frame, frame_gray, CV_BGR2GRAY );
    equalizeHist( frame_gray, frame_gray );

    // Détection des visages
    face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    intermediateImage = frame.clone ( );

     // Rectangles
    for( int i = 0; i < faces.size(); i++ )
    {
        Point p1 ( faces[i].x, faces[i].y );
        Point p2 ( faces[i].x + faces[i].width, faces[i].y + faces[i].height );
        rectangle ( intermediateImage,p1, p2, Scalar( 0, 255, 255 ), 4, 8 ,0 );
    }

    endImage = frame.clone ( );
    // Nez
    for( int i = 0; i < faces.size(); i++ )
    {
        Point center ( faces[i].x + faces[i].width/2, faces[i].y + 3*faces[i].height/5);
        Point center2 ( faces[i].x + 0.53*faces[i].width, faces[i].y + 0.56*faces[i].height);
        circle(endImage, center, faces[i].width/9, Scalar(0,0,138), -1,8,0);
        circle(endImage, center, faces[i].width/10, Scalar(0,0,255), -1,8,0);
        circle(endImage, center2, faces[i].width/28, Scalar(120,114,243), -1,8,0);
    }
}
